package com.tian.core.handle;

import com.tian.core.beans.ResultBean;
import com.tian.core.exceptions.CheckException;

import com.tian.core.lang.ErrorDefine;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 作用于整个Spring工程. ControllerAdvice 注解定义了一个全局的异常处理器.
 */
@ControllerAdvice
public class ControllerExceptionHandler {

	/**
	 * 对controller层抛出的的CheckException异常进行处理
	 */
	@ExceptionHandler(CheckException.class)
	@ResponseBody
	public ResultBean processCheckException(CheckException e) {
		ResultBean result = new ResultBean();
		result.setRespCode(ErrorDefine.CHECK_FAIL);
		result.setRespMsg(e.getMessage());
		return result;
	}
	/**
	 * 对spring boot 默认异常进行处理
	 */
	@ExceptionHandler(NoHandlerFoundException.class)
	@ResponseBody
	public ResultBean notFound() {
		ResultBean result = new ResultBean();
		result.setRespCode(ErrorDefine.BAD_REQUEST);
		result.setRespMsg(ErrorDefine.BAD_REQUEST_MSG);
		return result;
	}
}